package com.mr.qa.bo;

import java.util.List;

import com.mr.qa.dao.impl.ShopArticleDAO;
import com.mr.qa.dao.impl.ShopCategoryDAO;

public class ShopCategory extends BaseBO {
	private String name;
	private String email;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List getAvailCategories(){
		ShopCategoryDAO categoryDAO = new ShopCategoryDAO();
		return categoryDAO.getAvaiCategories();
	}
	
	public void add(){
		ShopCategoryDAO categoryDAO = new ShopCategoryDAO();
		categoryDAO.save(this);
	}
	
	public ShopCategory getById(Integer id){
		ShopCategoryDAO categoryDAO = new ShopCategoryDAO();
		return categoryDAO.findById(id);
	}
	
	public void update(){
		ShopCategoryDAO categoryDAO = new ShopCategoryDAO();
		categoryDAO.modify(this);
	}
	
	public Integer countArticle(Integer id){
		ShopArticleDAO saDAO = new ShopArticleDAO();
		return saDAO.countArticlesByCategory(id);
	}
	
	/**
	 * 
	 * @return int
	 * 0: success
	 * 1: there's article exists in this category
	 */
	public int deleteCategory(){
		Integer articleCount = countArticle(id);
		if ( articleCount > 0 ){
			return 1;//1: there's article exists in this category
		}
		ShopCategoryDAO categoryDAO = new ShopCategoryDAO();
		categoryDAO.delete(this);
		return 0;
	}
}
